package com.runen.infinitemovement.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.DynamicStateNotificationAdapter;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.MyDynamicListEntity;
import com.runen.infinitemovement.widget.CustomLoadMoreView;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FocusNotificationFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.rv_dynamic_state_notification)
    RecyclerView rvDynamicStateNotification;
    @BindView(R.id.srl)
    SwipeRefreshLayout srl;
    private int position;
    private View view;
    private DynamicStateNotificationAdapter stateNotificationAdapter;
    private int page = 2;

    public FocusNotificationFragment(int position) {
        this.position = position;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.dynamic_state_notification, container, false);
            ButterKnife.bind(this, view);
            initView();
            initData();
        }
        return view;
    }

    private void initView() {
        srl.setOnRefreshListener(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        rvDynamicStateNotification.setLayoutManager(layoutManager);
        stateNotificationAdapter = new DynamicStateNotificationAdapter(getActivity(), position);
        stateNotificationAdapter.getLoadMoreModule().setLoadMoreView(new CustomLoadMoreView());
        rvDynamicStateNotification.setAdapter(stateNotificationAdapter);
        initLoadMore();
    }

    private void initLoadMore() {
        stateNotificationAdapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                loadMoreRequested();
            }
        });
        stateNotificationAdapter.getLoadMoreModule().setAutoLoadMore(true);
        stateNotificationAdapter.getLoadMoreModule().setEnableLoadMoreIfNotFullPage(false);
    }

    private void initData() {
        srl.setRefreshing(true);
        String string = "?limit=20&page=1&messageType=%s";
        new UserAPI().onGetMyDynamicList(String.format(string, position + 1), new BaseResultCallback<MyDynamicListEntity>() {
            @Override
            public void onResponse(MyDynamicListEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        stateNotificationAdapter.setNewData(response.data);
                        if (stateNotificationAdapter.getData().size() >= response.count) {
                            stateNotificationAdapter.getLoadMoreModule().loadMoreEnd(true);
                            stateNotificationAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        page = 2;
        initData();
    }

    public void loadMoreRequested() {
        String string = "?limit=20&page=%s&messageType=%s";
        new UserAPI().onGetMyDynamicList(String.format(string, page, position + 1), new BaseResultCallback<MyDynamicListEntity>() {
            @Override
            public void onResponse(MyDynamicListEntity response) {
                srl.setRefreshing(false);
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        stateNotificationAdapter.addData(response.data);
                        if (stateNotificationAdapter.getData().size() >= response.count) {
                            stateNotificationAdapter.getLoadMoreModule().loadMoreEnd(true);
                            stateNotificationAdapter.getLoadMoreModule().setEnableLoadMore(false);
                        } else {
                            page++;
                            stateNotificationAdapter.getLoadMoreModule().loadMoreComplete();
                            stateNotificationAdapter.getLoadMoreModule().setEnableLoadMore(true);
                        }
                    }
                }
            }
        });
    }
}
